package com.example.springbootdemo.controller.jsp;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: zyb
 * @Date: 2018/10/29 14:34
 * @Description: jsp页面使用
 */

//返回的是页面时，使用此注解，使用RESTController时会解析为json字符串
@Controller
public class HtmlController {

    @GetMapping("/jsp")
    public String index(@RequestParam(value = "jsp") String jsp, HttpServletRequest request) {
        String username = request.getParameter("username");
        String passwd = request.getParameter("passwd");
        if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(passwd))
            System.out.println("username : " + username + ", passwd : " + passwd);
        return jsp;
    }

    @RequestMapping(value = "/login", method = {RequestMethod.GET, RequestMethod.POST})
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String passwd = request.getParameter("passwd");
        System.out.println("username : " + username + ", passwd : " + passwd);
        if ("zyb".equals(username) && "123".equals(passwd)) {
            response.getWriter().print("success");
        } else {
            response.getWriter().print("fail");
        }
    }
}
